package com.controller;

import com.domain.BlogArticle;
import com.domain.BlogComment;
import com.domain.Statistic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.BlogArticleMapper;
import com.mapper.CommentMapper;
import com.mapper.StatisticMapper;
import com.service.CommentService;
import com.service.SiteService;
import com.utils.Commons;
import com.vdurmont.emoji.EmojiParser;
import io.micrometer.common.util.StringUtils;
import io.netty.util.internal.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    BlogArticleMapper blogArticleMapper;
    @Autowired
    StatisticMapper statisticMapper;
    @Autowired
    Commons commons;

    public List<BlogArticle> getHeatArticles( ){
        List<Statistic> list = statisticMapper.getStatistic();
        List<BlogArticle> articlelist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++){
                BlogArticle article = blogArticleMapper.selectArticleWithId(list.get(i).getArticleId());
                article.setHits(list.get(i).getHits());
                article.setCommentsNum(list.get(i).getCommentsNum());
                articlelist.add(article);
                if(i>=9){
                    break;
                }
        }
        return articlelist;
    }
    public PageInfo<BlogArticle> selectArticleWithPage(Integer page, Integer count){
        PageHelper.startPage(page,count);
        List<BlogArticle> articleList = blogArticleMapper.selectArticleWithPage();
        for (int i = 0; i< articleList.size(); i++){
            BlogArticle article = articleList.get(i);
            Statistic statistic = statisticMapper.selectStatisticArticleWithId(article.getId());
            article.setHits(statistic.getHits());
            article.setCommentsNum(statistic.getCommentsNum());
        }
        PageInfo<BlogArticle> pageInfo=new PageInfo<>(articleList);
        return pageInfo;
    }
    @GetMapping(value = "/")
    private String index(Model model){return this.index(model, 1, 5);}
    @GetMapping(value = "/page/{p}")
    public String index(Model model, @PathVariable("p") int page, @RequestParam(value = "count",defaultValue = "5")int count ){
//        PageHelper.startPage(page,count);
        PageInfo<BlogArticle> articles = selectArticleWithPage(page,count);
            List<BlogArticle> articleList = getHeatArticles();
            model.addAttribute("articles",articles);
            model.addAttribute("articleList",articleList);
            return "client/index1";
    }
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    CommentService commentService;
    @Autowired
    SiteService siteService;
    @Autowired
    CommentMapper commentMapper;

    public void publish(BlogArticle article){
        article.setContent(EmojiParser.parseToAliases(article.getContent()));
        article.setCreated(new Date());
        article.setHits(0);
        article.setCommentsNum(0);
        blogArticleMapper.publishArticle(article);
        statisticMapper.addStatistic(article);
    }
    public void updateArticleWithId(BlogArticle article){
        article.setModified(new Date());
        blogArticleMapper.updateArticleWithId(article);
    }
    public void deleteArticleWithId(int id){
        blogArticleMapper.deleteArticleWithId(id);
        statisticMapper.deleteStatisticWithId(id);
        commentMapper.deleteCommentWithId(id);
    }
    private void getArticleComments(HttpServletRequest request,BlogArticle article){
        if(article.getAllowComment()){
            String cp = request.getParameter("cp");
            cp = StringUtils.isBlank(cp) ? "1" : cp;
            request.setAttribute("cp",cp);
            PageInfo<BlogComment>comments = commentService.getComments(article.getId(),Integer.parseInt(cp),3);
            request.setAttribute("cp",cp);
            request.setAttribute("comment",comments);
            request.setAttribute("article",article);
        }
    }
    @GetMapping(value = "/article/{id}")
    public String getArticleById(@PathVariable("id") Integer id,HttpServletRequest request){
        BlogArticle article = blogArticleMapper.selectArticleWithId(id);
        if (article!=null){
            getArticleComments(request , article);
            siteService.updateStatistic(article);
            request.setAttribute("article",article);
            return "client/articleDetails";
        }else {
            logger.warn("查询文章详情结果为空，查询文章id："+id);
            return "comm/error_404";
        }
    }

}
